<template>
  <div id="lifeTopSearch">
    <h1 class="con-right-title">
      活动发布
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="活动分类" prop="tags">
          <el-select v-model="formInline.tags" clearable>
            <el-option
              v-for="item in tagsList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序规则" prop="sort">
          <el-select v-model="formInline.sort" placeholder="">
            <el-option
              v-for="item in sortList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动名称" prop="activityId">
          <el-input
            v-model="formInline.activityId"
            placeholder="请输入活动名称搜索"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="删除状态" prop="deleted">
          <el-select v-model="formInline.deleted">
            <el-option
              v-for="item in deletedList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容查询" prop="content">
          <el-input
            v-model="formInline.content"
            placeholder="请根据内容搜索"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="时间线" prop="timeLine">
          <el-select v-model="formInline.timeLine">
            <el-option
              v-for="item in timeLineList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容类型" prop="type">
          <el-select v-model="formInline.type" clearable>
            <el-option
              v-for="item in typeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人审结果" prop="manual">
          <el-select v-model="formInline.manual" clearable>
            <el-option
              v-for="item in pExamineList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机审结果" prop="machine">
          <el-select v-model="formInline.machine" clearable>
            <el-option
              v-for="item in mExamineList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'PostMTopSearch',
  msg: '活动帖子管理-我的发布-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          tags: '', //活动分类
          sort: 'createdDate desc', // 排序规则
          activityId: '', //活动名称
          deleted: false, // 删除状态
          content: '', // 内容查询
          timeLine: '10', // 时间线
          type: '', //内容类型
          manual: '', // 人审结果
          machine: '', // 机审结果
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      tagsList: [{ dictLabel: '全部', dictValue: '' }], //活动分类
      sortList: [
        { dictLabel: '活动帖子发布时间倒序', dictValue: 'createdDate desc' },
        { dictLabel: '热度由高到低', dictValue: 'hotValue desc' }
      ], // 排序规则(写死)
      deletedList: [
        { dictLabel: '未删除', dictValue: false },
        { dictLabel: '已删除', dictValue: true }
      ], //删除状态(写死)
      timeLineList: [], //帖子时间线
      typeList: [{ dictLabel: '全部', dictValue: '' }], //内容类型
      pExamineList: [
        // 人审结果(写死)
        { dictLabel: '全部', dictValue: '' },
        { dictLabel: '通过', dictValue: 'PASSED' },
        { dictLabel: '未审核', dictValue: 'NONE' }
      ],
      mExamineList: [
        // 机审结果(写死)
        { dictLabel: '全部', dictValue: '' },
        { dictLabel: '通过', dictValue: 'PASSED' },
        { dictLabel: '不通过', dictValue: 'REJECTED' }
      ],
      rules: {
        tags: {},
        sort: {},
        activityId: {},
        deleted: { type: 'boolean' },
        content: {},
        timeLine: {},
        type: {},
        manual: {},
        machine: {},
        createTime: { type: 'array' }
      }
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    },
    /* 获取数据状态 */
    async getStatusList() {
      // 活动帖子时间线
      let data1 = await sysNormalDisable('sys_jobwork_time')
      this.timeLineList = [...data1, { dictLabel: '全部', dictValue: '' }]
      // 活动分类
      let data2 = await sysNormalDisable('activity_type', 'tagsType')
      this.tagsList = this.tagsList.concat(data2)
      // 内容类型
      let data3 = await sysNormalDisable('sys_posts_type')
      this.typeList = this.typeList.concat(data3)
      //设置默认数据
      this.setDefaultValue()
      this.$emit('toSearch')
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.sort = this.sortList[0].dictValue
      this.formInline.deleted = this.deletedList[0].dictValue
      this.formInline.timeLine = this.timeLineList[0].dictValue
    }
  }
}
</script>

<style lang="less">
#lifeTopSearch {
  .el-input {
    &.el-input--suffix {
      width: 182px !important;
    }
  }

  label.el-form-item__label {
    width: 70px !important;
  }
}
</style>
